package com.example.menulhd;


import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private LeftFragment leftFragment;
    private TextView tv_recommend,tv_must_buy;
    private RightFragment rightFragment;
    private String[] names1={"食物11","食物12"};
    private String[] sales1={"销量11","销量12"};
    private String[] prices1={"售价11","售价12"};
    private int[] imgs1={R.drawable.recom_one,R.drawable.recom_two};

    private String[] names2={"食物21","食物22"};
    private String[] sales2={"销量21","销量22"};
    private String[] prices2={"售价21","售价22"};
    private int[] imgs2={R.drawable.must_buy_one,R.drawable.must_buy_two};

    private Map<String,List<FoodBean>> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setDate();
        init();
        clickEvent();
    }

    private void init() {
        fragmentManager = getSupportFragmentManager();
        leftFragment = (LeftFragment) fragmentManager.findFragmentById(R.id.left);
        tv_recommend = leftFragment. getView().findViewById(R.id.tv_recommend);
        tv_must_buy = leftFragment.getView().findViewById(R.id.tv_must_buy);
        switchData(map.get("1"));
    }

    private void setDate() {
        map = new HashMap<>();
        List<FoodBean> list1= new ArrayList<>();
        List<FoodBean> list2= new ArrayList<>();
        for (int i= 0;i < names1.length;i++){
            FoodBean bean=new FoodBean();
            bean.setName(names1[i]);
            bean.setSales(sales1[i]);
            bean.setPrice(prices1[i]);
            bean.setImg(imgs1[i]);
            list1.add(bean);
        }
        map.put("1",list1);
        for (int i= 0;i < names2.length;i++){
            FoodBean bean=new FoodBean();
            bean.setName(names2[i]);
            bean.setSales(sales2[i]);
            bean.setPrice(prices2[i]);
            bean.setImg(imgs2[i]);
            list2.add(bean);
        }
        map.put("2",list2);
    }
    private void clickEvent() {
        tv_recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchData (map.get("1"));
                tv_recommend.setBackgroundColor(Color.WHITE);
                tv_must_buy.setBackgroundResource(R.color.gray);
            }
        });
        tv_must_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switchData (map.get("2"));
                tv_must_buy.setBackgroundColor(Color.WHITE);
                tv_recommend.setBackgroundResource(R.color.gray);
            }
        });

    }

    private void switchData(List<FoodBean> list) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction =fragmentManager.beginTransaction();
        rightFragment = new RightFragment().getInstance(list);
        fragmentTransaction.replace(R.id.right,rightFragment);
        fragmentTransaction.commit();
    }
}